package com.yanjiali.controller;

import com.yanjiali.enumerate.FileTypeEnum;
import com.yanjiali.model.req.FileUploadReq;
import com.yanjiali.model.req.WebDirectReq;
import com.yanjiali.model.resp.FileUploadResp;
import com.yanjiali.model.resp.WebDirectResp;
import com.yanjiali.model.R;
import com.yanjiali.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Package: com.yanjiali.controller
 * @Author: yanjiali
 * @Created: 2025/4/13 20:14
 */
@RestController
@Api(tags = "文件上传控制器")
@RequestMapping("/v1/files")
public class FileController {

    @Autowired
    private FileUploadService fileUploadService;

    @PostMapping("/presigned-url")
    @ApiOperation("web直传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webDirectReq", value = "web直传参数")
    })
    public R<WebDirectResp> generatePresignedUrl(@RequestBody WebDirectReq webDirectReq) {
        FileTypeEnum fileTypeEnum = FileTypeEnum.valueOf(webDirectReq.getFileType().toUpperCase());
        WebDirectResp webDirectResp = fileUploadService
                .generatePresignedUrl(webDirectReq.getFilename(), fileTypeEnum, webDirectReq.getExpirySeconds());
        return R.ok(webDirectResp);
    }

    @PostMapping("/upload")
    @ApiOperation("文件普通上传接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileUploadReq", value = "文件普通上传参数")
    })
    public R<FileUploadResp> fileUpload(@ModelAttribute FileUploadReq fileUploadReq) throws IOException {
        FileUploadResp fileUploadResp = fileUploadService.fileUpload(fileUploadReq);
        return R.ok(fileUploadResp);
    }
}
